package com.zz.cinema.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zz.cinema.domain.TS_Customer;
import com.zz.cinema.service.ITS_CustomerService;

@Controller
@RequestMapping("/admin")
public class TS_AdminController {

	@Resource
	private ITS_CustomerService customerService;

	@RequestMapping("/aa")
	public String adminIndex() {

		return "admin/index";
	}

	@RequestMapping("/viewCustomer/{id}")
	public ModelAndView selectCustomer(@PathVariable("id") int id) {

		ModelAndView mv = new ModelAndView();
		TS_Customer customer = customerService.queryCustomer(id);
		mv.addObject("selCustomer", customer);
		mv.setViewName("redirect:/admin/listCustomer");
		return mv;
	}

	@RequestMapping("/listCustomer/{page}")
	public ModelAndView pageCustomer(@PathVariable(required = false, name = "page") int page) {

		ModelAndView mv = new ModelAndView();
		mv.addObject("customerPage", customerService.getPage(page == 0 ? 1 : page));
		mv.setViewName("admin/basic_tables");
		return mv;
	}

	@RequestMapping("/listCustomer")
	public String listCustomer() {

		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		// customerService.addCustomer(new TS_Customer("aa", "aa", "aa", 111, 1,
		// 111));
		ModelAndView mv = new ModelAndView();
		mv.addObject("customerPage", customerService.getPage(1));
		mv.setViewName("admin/basic_tables");
		return "redirect:/admin/listCustomer/1";
	}

	@RequestMapping("/deleteCustomer/{id}")
	public ModelAndView deleteCustomer(@PathVariable("id") int id) {

		boolean status = customerService.deleteCustomer(id);
		ModelAndView mv = new ModelAndView();
		mv.addObject("isDelete", status);
		mv.setViewName("redirect:/admin/listCustomer/1");
		return mv;
	}

	@RequestMapping("/editCustomer/{id}")
	public ModelAndView updateCustomer(@PathVariable("id") int id) {

		ModelAndView mv = new ModelAndView();
		TS_Customer customer = customerService.queryCustomer(id);
		
		if (customer == null) {
			mv.setViewName("redirect:/admin/listCustomer");
		} else {
			mv.addObject("id", id);
			mv.setViewName("admin/validation");
		}
		
		return mv;
	}

	@ResponseBody
	@RequestMapping("/getCustomer/{id}")
	public TS_Customer getCustomer(@PathVariable("id") int id) {

		TS_Customer customer = customerService.queryCustomer(id);
		System.out.println(customer);
	
		return customer;
	}

}
